# Copyright WestYao.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# the minimum version of CMake.
cmake_minimum_required(VERSION 3.5.0)
project(helloAVCodec)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
set(SAMPLE_FRAMEWORK_PATH ${CMAKE_CURRENT_SOURCE_DIR}/sample_framework)
set(BOUNDS_CHECKING_FUNCTION_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bounds_checking_function)

# clone video codec demo code
if(NOT EXISTS ${SAMPLE_FRAMEWORK_PATH})
message(STATUS "sample_framework directory does not exist, do clone code")
execute_process(COMMAND git config core.sparsecheckout true)
execute_process(COMMAND git clone --no-checkout --filter=blob:none --sparse https://gitee.com/westyao/multimedia_av_codec.git ${SAMPLE_FRAMEWORK_PATH})
file(WRITE ${SAMPLE_FRAMEWORK_PATH}/.git/info/sparse-checkout "test/unittest/video_test/video_test/\n")
execute_process(COMMAND git -C ${SAMPLE_FRAMEWORK_PATH} checkout dev_vcd)
endif()

# clone bounds checking function code
if(NOT EXISTS ${BOUNDS_CHECKING_FUNCTION_PATH})
message(STATUS "bounds_checking_function directory does not exist, do clone code")
execute_process(COMMAND git clone https://gitee.com/openharmony/third_party_bounds_checking_function.git ${BOUNDS_CHECKING_FUNCTION_PATH})
endif()

if((NOT EXISTS ${SAMPLE_FRAMEWORK_PATH}) OR (NOT EXISTS ${BOUNDS_CHECKING_FUNCTION_PATH}))
message(FATAL_ERROR "main directory does not exist, exiting.")
endif()

set(NDK_PATH $ENV{OHOS_SDK_NATIVE})
set(SAMPLE_PATH ${SAMPLE_FRAMEWORK_PATH}/test/unittest/video_test/video_test)
add_definitions(-DSAMPLE_BUILD_TO_HAP)

# NDK directory
include_directories(
    ${NDK_PATH}/sysroot/usr/include/
    ${NDK_PATH}/sysroot/usr/include/multimedia/player_framework
    ${NDK_PATH}/sysroot/usr/include/native_buffer
    ${NDK_PATH}/sysroot/usr/include/native_window
    ${NDK_PATH}/sysroot/usr/include/native_image
)

include_directories(
    ${NATIVERENDER_ROOT_PATH}
    ${NATIVERENDER_ROOT_PATH}/common/dfx
    ${NATIVERENDER_ROOT_PATH}/bounds_checking_function/include
    ${NATIVERENDER_ROOT_PATH}/xcomponent_window_wrapper
    ${NATIVERENDER_ROOT_PATH}/napi_wrapper/sample
    ${SAMPLE_PATH}/capbilities/capbility_node/data_producer/bitstream_reader
    ${SAMPLE_PATH}/capbilities/capbility_node/data_producer/data_producer_base
    ${SAMPLE_PATH}/capbilities/capbility_node/data_producer/demuxer
    ${SAMPLE_PATH}/capbilities/capbility_node/data_producer/raw_data_reader
    ${SAMPLE_PATH}/capbilities/capbility_node/video_codec/video_codec
    ${SAMPLE_PATH}/capbilities/capbility_node/video_codec/video_decoder
    ${SAMPLE_PATH}/capbilities/capbility_node/video_codec/video_encoder
    ${SAMPLE_PATH}/capbilities/sample_buffer_queue
    ${SAMPLE_PATH}/capbilities/sample_window_manager
    ${SAMPLE_PATH}/capbilities/sample_window_manager/window_wrapper/include
    ${SAMPLE_PATH}/common/include
    ${SAMPLE_PATH}/sample/base
    ${SAMPLE_PATH}/sample/yuv_viewer
    ${SAMPLE_PATH}/sample/decoder
    ${SAMPLE_PATH}/sample/encoder
    ${SAMPLE_PATH}/sample/helper/include
)

set(SOURCE_FILES
# napi wrapper code (interface)
    napi_wrapper/napi_init.cpp
    napi_wrapper/sample/sample_napi_wrapper.cpp

# bounds checking function code
    ${BOUNDS_CHECKING_FUNCTION_PATH}/src/memcpy_s.c

# dfx adapter code
    common/dfx/avcodec_trace_adapter.cpp

# xcomponent window wrapper code
    xcomponent_window_wrapper/xcomponent_window_wrapper.cpp

# sample code
    ${SAMPLE_PATH}/capbilities/capbility_node/data_producer/bitstream_reader/bitstream_reader.cpp
    ${SAMPLE_PATH}/capbilities/capbility_node/data_producer/data_producer_base/data_producer_base.cpp
    ${SAMPLE_PATH}/capbilities/capbility_node/data_producer/demuxer/demuxer.cpp
    ${SAMPLE_PATH}/capbilities/capbility_node/data_producer/raw_data_reader/rawdata_reader.cpp
    ${SAMPLE_PATH}/capbilities/capbility_node/video_codec/video_codec/codec_callback.cpp
    ${SAMPLE_PATH}/capbilities/capbility_node/video_codec/video_codec/video_codec_base.cpp
    ${SAMPLE_PATH}/capbilities/capbility_node/video_codec/video_decoder/video_decoder.cpp
    ${SAMPLE_PATH}/capbilities/capbility_node/video_codec/video_encoder/video_encoder.cpp
    ${SAMPLE_PATH}/capbilities/sample_buffer_queue/sample_buffer_queue.cpp
    ${SAMPLE_PATH}/capbilities/sample_buffer_queue/sample_buffer_queue_window_handler.cpp
    ${SAMPLE_PATH}/capbilities/sample_window_manager/sample_window_manager.cpp
    ${SAMPLE_PATH}/capbilities/sample_window_manager/window_wrapper/native_image_window_wrapper.cpp
    ${SAMPLE_PATH}/capbilities/sample_window_manager/window_wrapper/window_wrapper.cpp
    ${SAMPLE_PATH}/common/format_container.cpp
    ${SAMPLE_PATH}/common/sample_utils.cpp
    ${SAMPLE_PATH}/sample/base/sample_base.cpp
    ${SAMPLE_PATH}/sample/base/video_sample_base.cpp
    ${SAMPLE_PATH}/sample/decoder/video_decoder_sample.cpp
    ${SAMPLE_PATH}/sample/encoder/video_encoder_sample.cpp
    ${SAMPLE_PATH}/sample/helper/arg_checker.cpp
    ${SAMPLE_PATH}/sample/helper/arg_parser.cpp
    ${SAMPLE_PATH}/sample/helper/sample_helper.cpp
    ${SAMPLE_PATH}/sample/yuv_viewer/yuv_viewer.cpp
)

set(DEPENDS_LIBS
# napi
    libace_napi.z.so
    libace_ndk.z.so

# dfx
    libhilog_ndk.z.so
    libhitrace_ndk.z.so

# graphics
    libnative_buffer.so
    libnative_window.so
    libnative_image.so

# multimedia
    libnative_media_avdemuxer.so
    libnative_media_avmuxer.so
    libnative_media_avsource.so
    libnative_media_codecbase.so
    libnative_media_core.so
    libnative_media_vdec.so
    libnative_media_venc.so
)

add_library(avcodecdemo SHARED ${SOURCE_FILES})
target_link_libraries(avcodecdemo PUBLIC ${DEPENDS_LIBS})